package com.freemalll.merchant.controller;

import com.freemalll.common.core.annotation.ResultInterceptor;
import com.freemalll.common.security.utils.SecurityUtils;
import com.freemalll.merchant.common.cache.CacheContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * 登录相关 Controller
 * @author qinchuandong
 * @date 2025/10/22
 */
@Slf4j
@Tag(name = "登录")
@RestController
@RequestMapping("account")
@ResultInterceptor
@AllArgsConstructor
public class LoginController {
    private final CacheContext cacheContext;

    @Operation(summary = "获取登录的用户信息")
    @GetMapping("userInfo")
    public Map<String, Object> userInfo() {
        Map<String, Object> resultMap = new HashMap<>();
        String userId = SecurityUtils.getUserId();
        resultMap.put("username", SecurityUtils.getUsername());
        resultMap.put("permissions", cacheContext.getButtons(userId));
        resultMap.put("accountId", userId);
        return resultMap;
    }

    @Operation(summary = "获取资源")
    @GetMapping("getResource")
    public Map<String, Object> getResource() {
        return cacheContext.getResource(SecurityUtils.getUserId());
    }
}
